package org.lcx.pipeline.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.support.ManagedMap;

import org.lcx.pipeline.PipelineManager;
import org.lcx.pipeline.bean.PipelineExeption;
import org.lcx.pipeline.bean.PipelineResult;

/**
 * PipelineManger接口实现类<br>
 * 实现pipeline接口,并实现所有的业务逻辑方法
 */
public class PipelineManagerImpl implements PipelineManager {
	public final Log log = LogFactory.getLog(this.getClass());
	// id
	private String id;
	// name
	private String name;
	// 存放pipeline中定义的所有chain, key:PipelineChain的name;value:PipelineChain对象
	public ManagedMap<String, PipelineChain> pipelineChains;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ManagedMap<String, PipelineChain> getPipelineChains() {
		return pipelineChains;
	}

	public void setPipelineChains(ManagedMap<String, PipelineChain> pipelineChains) {
		this.pipelineChains = pipelineChains;
	}

	/**
	 * 执行pipeline chain处理
	 */
	public PipelineResult runProcess(String pChainName, Object pParam) throws PipelineExeption {
		PipelineResult result = new PipelineResultImpl();
		PipelineChain pc = this.getChainByName(pChainName);

		if (pc == null) {
			log.error("没有PipelineChain(" + pChainName + ")对应的定义");
			throw new PipelineExeption("没有PipelineChain(" + pChainName + ")对应的定义");
		} else {
			// 执行chain的处理
			pc.runProcess(result, pParam);
		}
		return result;
	}

	/**
	 * 根据chain name获取chain信息
	 * 
	 * @param pChainName
	 * @return
	 */
	private PipelineChain getChainByName(String pChainName) {
		if (this.pipelineChains == null) {
			return null;
		}
		return this.pipelineChains.get(pChainName);
	}

	@Override
	public String toString() {
		return "PipelineManagerImpl{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", pipelineChains="
				+ pipelineChains + '}';
	}

}
